from tkinter import Tk, Label, Button
from tkinter.filedialog import askopenfilenames
from PIL import Image, ImageTk
import os

# 存储图片路径列表
image_paths = []
current_index = 0


def open_images():
    global image_paths, current_index
    file_paths = askopenfilenames(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.bmp")])
    if file_paths:
        image_paths = list(file_paths)
        show_image()


def show_image():
    global image_paths, current_index, img_label
    if image_paths:
        image_path = image_paths[current_index]
        image = Image.open(image_path)
        photo = ImageTk.PhotoImage(image)
        img_label.config(image=photo)
        img_label.image = photo


def next_image():
    global current_index
    current_index = (current_index + 1) % len(image_paths)
    show_image()


def prev_image():
    global current_index
    current_index = (current_index - 1) % len(image_paths)
    show_image()


root = Tk()

open_btn = Button(root, text="打开图片", command=open_images)
open_btn.pack()

prev_btn = Button(root, text="上一张", command=prev_image)
prev_btn.pack()

next_btn = Button(root, text="下一张", command=next_image)
next_btn.pack()

img_label = Label(root)
img_label.pack()

root.mainloop()